package com.yxhl.zoume.core.func.timepicker;

import android.content.Context;
import android.text.TextUtils;

import com.bigkoo.pickerview.OptionsPickerView;
import com.yxhl.zoume.core.func.timepicker.view.ZMTimePickerView;

import java.util.ArrayList;


/**
 * Created by sugarya on 16/8/12.
 * a wrapper of OptionsPickerView as base class
 */
public abstract class ZMTimePicker {

    private static final String TAG = "ZMTimePicker";
    protected ArrayList<String> mOptions1Data;
    protected ArrayList<ArrayList<String>> mOptions2Data;
    protected ArrayList<ArrayList<ArrayList<String>>> mOptions3Data;
    private OptionsPickerView<String> mOptionsPickerView;
    /**
     * 是否显示时间选项框
     */
    private boolean enableShow = false;


    public ZMTimePicker() {
    }


    public ZMTimePicker getZMTimePicker(Context context, boolean cyclic1, boolean cyclic2, boolean cyclic3) {

        mOptionsPickerView = new OptionsPickerView<>(context);

        mOptions1Data = getOptions1Data();
        mOptions2Data = getOptions2Data();
        mOptions3Data = getOptions3Data();

        if (mOptions1Data != null && mOptions2Data != null && mOptions3Data != null) {
            mOptionsPickerView.setPicker(getOptions1Data(), getOptions2Data(), getOptions3Data(), true);
            mOptionsPickerView.setCyclic(cyclic1, cyclic2, cyclic3);
            enableShow = true;
        } else if (mOptions1Data != null && mOptions2Data != null) {
            mOptionsPickerView.setPicker(getOptions1Data(), getOptions2Data(), true);
            mOptionsPickerView.setCyclic(cyclic1, cyclic2, false);
            enableShow = true;
        } else if (mOptions1Data != null) {
            mOptionsPickerView.setPicker(getOptions1Data());
            mOptionsPickerView.setCyclic(cyclic1, false, false);
            enableShow = true;
        }

        mOptionsPickerView.setSelectOptions(1, 0, 0);

        return this;
    }

    public ZMTimePicker getZMTimePicker(Context context, boolean cyclic1, boolean cyclic2) {

        return getZMTimePicker(context, cyclic1, cyclic2, false);
    }

    public ZMTimePicker getZMTimePicker(Context context, boolean cyclic1) {

        return getZMTimePicker(context, cyclic1, false, false);
    }

    public ZMTimePicker getZMTimePicker(Context context) {

        return getZMTimePicker(context, false, false, false);
    }


    /**
     * 展示时间选项框
     *
     * @return
     * @throws IllegalStateException
     */
    public ZMTimePicker showTimePicker() throws IllegalStateException {
        if (enableShow) {
            mOptionsPickerView.show();
            return this;
        } else {
            throw new IllegalStateException("you must initialize one option at least");
        }
    }

    /**
     * 选择选项的结果监听
     *
     * @param zmTimePickerView
     */
    public void setOnOptionsSelectListener(final String zmTimePickerSimpleName, final ZMTimePickerView zmTimePickerView) {
        mOptionsPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3) {
                if (enableShow) {
                    String options1SelectResult = "";
                    if (mOptions1Data != null && options1 >= 0 && options1 < mOptions1Data.size()) {
                        options1SelectResult = mOptions1Data.get(options1);
                    }

                    String options2SelectResult = "";
                    if (mOptions2Data != null && options1 >= 0 && options1 < mOptions2Data.size()) {
                        ArrayList<String> arrayList = mOptions2Data.get(options1);
                        if (arrayList != null && options2 >= 0 && options2 < arrayList.size()) {
                            options2SelectResult = arrayList.get(options2);
                        }
                    }

                    String options3SelectResult = "";
                    if (mOptions3Data != null && options1 >= 0 && options1 < mOptions3Data.size()) {
                        ArrayList<ArrayList<String>> arrayLists = mOptions3Data.get(options1);
                        if (arrayLists != null && options2 >= 0 && options2 < arrayLists.size()) {
                            ArrayList<String> stringList = arrayLists.get(options2);
                            if (stringList != null && options3 >= 0 && options3 < stringList.size()) {
                                options3SelectResult = stringList.get(options3);
                            }
                        }
                    }


                    //数据回调
                    if (!TextUtils.isEmpty(options1SelectResult)
                            && !TextUtils.isEmpty(options2SelectResult)
                            && !TextUtils.isEmpty(options3SelectResult)) {
                        zmTimePickerView.renderZMTimePickerOptionSelectResult(zmTimePickerSimpleName, options1SelectResult, options2SelectResult, options3SelectResult);
                    } else if (!TextUtils.isEmpty(options1SelectResult)
                            && !TextUtils.isEmpty(options2SelectResult)) {
                        zmTimePickerView.renderZMTimePickerOptionSelectResult(zmTimePickerSimpleName, options1SelectResult, options2SelectResult, "");
                    } else if (!TextUtils.isEmpty(options1SelectResult)) {
                        zmTimePickerView.renderZMTimePickerOptionSelectResult(zmTimePickerSimpleName, options1SelectResult, "", "");
                    }
                }
            }
        });
    }


    /**
     * 初始化选项1数据
     *
     * @return
     */
    protected abstract ArrayList<String> getOptions1Data();

    /**
     * 初始化选项2数据
     *
     * @return
     */
    protected abstract ArrayList<ArrayList<String>> getOptions2Data();

    /**
     * 初始化选项3数据
     *
     * @return
     */
    protected abstract ArrayList<ArrayList<ArrayList<String>>> getOptions3Data();


}
